package com.thomas.async.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * @author thomas_liu
 * @date  2018/8/3 16:12
 * @desciption Task
 * @blog http://blog.thomas8341.com
 */
@Component
public class TaskAsync {

    public static Random random = new Random();
    public static Log log = LogFactory.getLog(TaskAsync.class);

    @Async
    public Future<String> doTaskOne() throws  Exception{
        log.info("---------开始做任务一---------");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务一耗时："+(end-start)+"毫秒");
        return new AsyncResult<>("------任务一完成------");
    }

    @Async
    public Future<String> doTaskTwo() throws  Exception{
        log.info("---------开始做任务二---------");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务二耗时："+(end-start)+"毫秒");
        return new AsyncResult<>("------任务二完成----");
    }

    @Async
    public Future<String> doTaskThree() throws  Exception{
        log.info("---------开始做任务三---------");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务三耗时："+(end-start)+"毫秒");
        return new AsyncResult<>("-------任务三完成------");
    }

}
